package tr.edu.metu.is.mega.client.course.activity;

import tr.edu.metu.is.mega.client.core.EditDialog;
import tr.edu.metu.is.mega.client.resource.MEGAResourceBundle;
import tr.edu.metu.is.mega.client.resource.MEGAResources;
import tr.edu.metu.is.mega.client.schedule.ActivityTemplateDialog;
import tr.edu.metu.is.mega.client.widgets.MegaDateTimeField;
import tr.edu.metu.is.mega.client.widgets.MegaTextField;
import tr.edu.metu.is.mega.common.dto.ActivityDTO;

import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.BeanModelFactory;
import com.extjs.gxt.ui.client.data.BeanModelLookup;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.layout.FormData;

/**
 * Edit dialog for {@link ActivityDTO}.
 */
public class ActivityEditDialog extends EditDialog<ActivityDTO> {

    private MEGAResourceBundle resourceBundle = MEGAResources.RESOURCEBUNDLE;

    private FormBinding binding;

    private BeanModelFactory beanModelFactory;

    /**
     * Default constructor.
     */
    public ActivityEditDialog() {
        super(ActivityDTO.class.getName());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void initForm(FormPanel parent, Object... params) {

        setHeight(150);
        setHeading("Edit Activity");
        resourceBundle = MEGAResources.RESOURCEBUNDLE;
        beanModelFactory = BeanModelLookup.get().getFactory(ActivityDTO.class);

        parent.add(ActivityTemplateDialog.createTypeComboBox(ActivityDTO.FIELD_TYPE), new FormData("96%"));

        MegaTextField descriptionTextField = new MegaTextField(ActivityDTO.FIELD_DESCRIPTION);
        descriptionTextField.setFieldLabel(resourceBundle.activityTemplateDescription());
        parent.add(descriptionTextField, new FormData("96%"));

        MegaDateTimeField dueDateField = new MegaDateTimeField(ActivityDTO.FIELD_DUEDATE);
        dueDateField.setFieldLabel(resourceBundle.activityTemplateDueDate());
        parent.add(dueDateField, new FormData("96%"));

        MegaDateTimeField alertTimeField = new MegaDateTimeField(ActivityDTO.FIELD_ALERT_TIME);
        alertTimeField.setFieldLabel(resourceBundle.activityTemplateAlertTime());
        parent.add(alertTimeField, new FormData("96%"));

        binding = new FormBinding(parent, true);

        ActivityDTO dto = new ActivityDTO();

        binding.bind(beanModelFactory.createModel(dto));

    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected ActivityDTO guiToDTO() {
        BeanModel model = (BeanModel) binding.getModel();
        return model.getBean();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void dtoToGUI(ActivityDTO dto) {
        binding.bind(beanModelFactory.createModel(dto));
    }

}
